﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Adams.Admin.DataAccess.Security
{
    public static class Signup
    {
        public static Adams.Admin.DataTransfer.Security.Signup.UserInfo GetUserInfo(string email)
        {
            var data = new Adams.Admin.DataTransfer.Security.Signup.UserInfo();

            int count = DB.RunScalar(@"
SELECT COUNT(*)
FROM Console.Users
WHERE Email = @Email
", CommandType.Text,
                DB.CreateParameter("@Email", email, SqlDbType.VarChar, 50)).DbInt();

            data.NotExists = count == 0;

            return data;
        }

        public static void SaveUser(DataTransfer.Security.Signup.SaveUserInfo input)
        {
            DB.RunScalar(@"
INSERT INTO Console.Users (FirstName, LastName, Email, PasswordHash, PasswordSalt, IsActive, IsLocked)
VALUES (@FirstName, @LastName, @Email, @PasswordHash, @PasswordSalt, 0, 0)
", CommandType.Text,
                DB.CreateParameter("@FirstName", input.FirstName.ToNString(), SqlDbType.VarChar, 50),
                DB.CreateParameter("@LastName", input.LastName.ToNString(), SqlDbType.VarChar, 50),
                DB.CreateParameter("@Email", input.Email.ToNString(), SqlDbType.VarChar, 50),
                DB.CreateParameter("@PasswordHash", input.PasswordHash.ToNString(), SqlDbType.VarChar, 50),
                DB.CreateParameter("@PasswordSalt", input.PasswordSalt.ToNString(), SqlDbType.VarChar, 50));
        }
    }
}
